package cn.jimmie.learn.viewArt;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

import cn.jimmie.learn.viewArt.refreshview.RefreshLayout;
import cn.jimmie.learn.viewArt.refreshview.core.RefreshState;


/**
 * function :
 *
 * <p> Create Time:  2019年04月14日 17:38 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class ScrollRefreshViewActivity extends AppCompatActivity {

    RefreshLayout layout;
    private ScrollView scrollView;
    int count;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scroll_refresh_activity);

        scrollView = findViewById(R.id.scrollview);
        layout = findViewById(R.id.refresh_layout);

        layout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onPullRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ScrollRefreshViewActivity.this, "刷新结束", Toast.LENGTH_SHORT).show();
                        layout.stopPullRefresh();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ScrollRefreshViewActivity.this, "刷新结束", Toast.LENGTH_SHORT).show();

                        if (count > 2) {
                            layout.stopLoadMore(RefreshState.ResultState.END);
                            return;
                        }
                        count++;
                        scrollView.addView(createView());

                        layout.stopLoadMore(RefreshState.ResultState.SUCCESS);
                    }
                }, 2000);
            }
        });
    }

    private View createView() {
        return LayoutInflater.from(this).inflate(R.layout.item, null);
    }
}
